package com.emergency.db.service.impl.item;

import com.emergency.common.util.CommonUtils;
import com.emergency.common.util.IdWorker;
import com.emergency.db.dao.item.ItemDoseDao;
import com.emergency.db.domain.item.ItemDose;
import com.emergency.db.service.item.ItemDoseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class ItemDoseServiceImpl implements ItemDoseService {
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private ItemDoseDao itemDoseDao;

    @Override
    public Long save(ItemDose itemDose) {
        if (CommonUtils.isNullOrEmpty(itemDose.getId())) {
            itemDose.setId(idWorker.nextId());
            itemDose.setCreateTime(new Date());
        }
        itemDoseDao.save(itemDose);
        return itemDose.getId();
    }

    @Override
    public void deleteAllByItem(Long id) {
        itemDoseDao.deleteAllByItem(id);
    }

    //分页查询
    @Override
    public Page<ItemDose> findList(Map searchMap, int page, int size, Long id) {
        Sort sort = Sort.by(Sort.Direction.DESC, "create_time");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return itemDoseDao.findAllByItem(id, pageable);
    }
}
